﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using FirstFloor.ModernUI.Windows.Controls;

namespace SimlvClient.Pages
{
    /// <summary>
    /// Interaction logic for ImgList.xaml
    /// </summary>
    public partial class ImgList : ModernDialog
    {
        public List<Model.NewsImg> NewImageModel { get; set; }
        public ImgList(List<Model.NewsImg> NewImage)
        {
            NewImageModel = NewImage;
            InitializeComponent();
            GetData();
        }

        public void GetData()
        {
            gridNews.ItemsSource = NewImageModel;
            gridNews.Items.Refresh();
        }

        private void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (gridNews.SelectedItem != null)
            {
                SelectModel = gridNews.SelectedItem as Model.NewsImg;
            }
            Thread thread = new Thread(new ThreadStart(DeletetoServer));
            thread.Start();
        }
        public Model.NewsImg SelectModel;

        private void DeletetoServer()
        {
            Dispatcher.Invoke(new Action(() =>
            {
                IsBusy.IsIndeterminate = true;
                IsBusy.Visibility = Visibility.Visible;
                CancelButton.IsEnabled = false;
                BtnDelete.IsEnabled = false;
            }));
            string ftpServerIP = ConfigurationManager.AppSettings["ftpServerIP"];
            string ftpUserID = ConfigurationManager.AppSettings["ftpUserID"]; ;
            string ftpPassword = ConfigurationManager.AppSettings["ftpPassword"];
            string ImageFolder = ConfigurationManager.AppSettings["NewsImage"];
            if (ImageFolder != "")
            {
                ImageFolder = ImageFolder + "/";
            }
            FTPHelper fhelper = new FTPHelper(ftpServerIP, ftpUserID, ftpPassword);
            if (SelectModel != null)
            {
                fhelper.DeleteFileName(SelectModel.ImageUrl.Replace(ConfigurationManager.AppSettings["website"] + ImageFolder, ""));
                SelectModel.Delete();
                NewImageModel.Remove(SelectModel);
            }
            Dispatcher.Invoke(new Action(() =>
           {
               BtnDelete.IsEnabled = true;
               IsBusy.IsIndeterminate = false;
               CancelButton.IsEnabled = true;
               IsBusy.Visibility = Visibility.Collapsed;
               GetData();
               ModernDialog.ShowMessageText("删除成功！");
           }));


        }
    }
}
